package com.mazewei.consumer.listener;

import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.Map;

// 将当前类放到SpringIOC容器中
@Component
public class SpringRabbitListener {
    // 监听名称simple.queue队列，Spring只要接收到该队列的消息就会接收消息
//    @RabbitListener(queues = "simple.queue")
//    // Spring自动将接收的消息给方法参数msg
//    public void listenSimpleQueueMessage(String msg) throws InterruptedException {
//        // 打印接收到的消息
//        System.out.println("spring 消费者接收到消息：【" + msg + "】");
//    }

    // 监听名称simple.queue队列，模拟工作队列的处理
//    @RabbitListener(queues = "simple.queue")
//    public void listenWorkQueue1(String msg) throws InterruptedException {
//        // 打印消费者1接收到的消息和当前时间
//        System.out.println("消费者1接收到消息：【" + msg + "】" + new Date());
//        // 模拟消息处理延迟
//        Thread.sleep(20);
//    }
//
//    // 监听名称simple.queue队列，模拟另一个工作队列的处理
//    @RabbitListener(queues = "simple.queue")
//    public void listenWorkQueue2(String msg) throws InterruptedException {
//        // 打印消费者2接收到的消息和当前时间
//        System.err.println("消费者2........接收到消息：【" + msg + "】" + new Date());
//        // 模拟消息处理延迟
//        Thread.sleep(100);
//    }
//    监听广播队列
//    @RabbitListener(queues = "fanout.queue1")
//    public void listenFanoutQueue1(String msg) {
//        // 打印消费者1接收到的广播消息
//        System.out.println("消费者1接收到Fanout消息：【" + msg + "】");
//    }
//
//    @RabbitListener(queues = "fanout.queue2")
//    public void listenFanoutQueue2(String msg) {
//        // 打印消费者2接收到的广播消息
//        System.out.println("消费者2接收到Fanout消息：【" + msg + "】");
//    }
//    监听路由
    /*
//       TODO:
//           1. value = @Queue(name = "direct.queue1") 表示绑定的第一个队列
//           2.exchange = @Exchange(name = "itcast.direct", type = ExchangeTypes.DIRECT) 表示交换机名和类型
//           3.key = {"red", "blue"} 表示路由key
//    */
//    @RabbitListener(bindings = @QueueBinding(
//            value = @Queue(name = "direct.queue1"),
//            exchange = @Exchange(name = "itcast.direct", type = ExchangeTypes.DIRECT),
//            key = {"red", "blue"}
//    ))
//    public void listenDirectQueue1(String msg) {
//        System.out.println("消费者接收到direct.queue1的消息：【" + msg + "】");
//    }
//
//    @RabbitListener(bindings = @QueueBinding(
//            value = @Queue(name = "direct.queue2"),
//            exchange = @Exchange(name = "itcast.direct", type = ExchangeTypes.DIRECT),
//            key = {"red", "yellow"}
//    ))
//    public void listenDirectQueue2(String msg) {
//        System.out.println("消费者接收到direct.queue2的消息：【" + msg + "】");
//    }
    /*
        TODO:
            1.value = @Queue(name = "topic.queue1") 表示绑定的第一个队列
            2.exchange = @Exchange(name = "itcast.topic", type = ExchangeTypes.TOPIC) 表示交换机名和类型
            3. key = "china.#" 表示路由key只要以china开始都会接收
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "topic.queue1"),
            exchange = @Exchange(name = "itcast.topic", type = ExchangeTypes.TOPIC),
            key = "china.#"
    ))
    public void listenTopicQueue1(String msg) {
        System.out.println("消费者接收到topic.queue1的消息：【" + msg + "】");
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "topic.queue2"),
            exchange = @Exchange(name = "itcast.topic", type = ExchangeTypes.TOPIC),
            key = "#.news"
    ))
    public void listenTopicQueue2(String msg) {
        System.out.println("消费者接收到topic.queue2的消息：【" + msg + "】");
    }

    @RabbitListener(queues = "object.queue")
    public void listenObjectQueue(Map<String, Object> msg) {
        System.out.println("收到消息：【" + msg + "】");
    }
}
